package cn.lg.soar.core.annotations.constraints.validator;

import cn.lg.soar.common.util.Validator;
import cn.lg.soar.common.util.data.StringUtil;
import cn.lg.soar.core.annotations.constraints.FixedPhone;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 校验器
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public class FixedPhoneValidator implements ConstraintValidator<FixedPhone, String> {

    private boolean required;

    @Override
    public void initialize(FixedPhone constraintAnnotation) {
        this.required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if(StringUtil.isBlank(s)){
            return !required;
        }
        return Validator.isFixedPhone(s);
    }
}
